package com.htg.dialog;


import android.content.Context;
import android.content.DialogInterface;
import android.view.View;
import android.widget.AdapterView;

import androidx.collection.ArrayMap;

import java.util.LinkedList;
import java.util.List;
import java.util.Queue;

public class AndroidDialog {

    private Context context;

    private DialogWaitting dialogWaitting;

    private Queue<Integer> queueWaitting;

    private DialogTextItems dialogTextItems;

    private DialogInput dialogInput;

    private DialogKeyValueTable dialogKeyValueTable;

    private DialogAgreementHint dialogAgreementHint;

    private DialogLoveHint dialogLoveHint;

    private DialogRealNameAuthAndTaxDeclaration dialogRealNameAuthAndTaxDeclaration;

    private DialogPandu dialogPandu;

    private DialogDonotKnowPosition dialogDonotKnowPosition;

    private DialogTaskCompleteState dialogTaskCompleteState;

    private DialogAMapDownloading dialogAMapDownloading;

    private DialogNewVersionHint dialogNewVersionHint;

    private DialogStandardOneButton dialogStandardOneButton;

    private DialogYunChaxun dialogYunChaxun;

    private DialogKeyValueInput dialogKeyValueInput;
    private int colorPrimary;
    private float cornerRadius;

    public AndroidDialog(Context context) {
        this.context = context;
        this.colorPrimary = context.getResources().getColor(R.color.blue_0166FF, context.getTheme());
        this.cornerRadius = context.getResources().getDimensionPixelSize(R.dimen.dp_5);
    }

    public void showDialogKeyValueInput(String content, String leftStr, String rightStr, DialogKeyValueInput.OnDialogKeyValueInputClickListener listenerLeft, DialogKeyValueInput.OnDialogKeyValueInputClickListener listenerRight){
        if (dialogKeyValueInput==null)
            dialogKeyValueInput = new DialogKeyValueInput(context);
        dialogKeyValueInput.show(content,leftStr,rightStr,listenerLeft,listenerRight);
    }

    public void showDialogYunChaxun(DialogYunChaxun.OnOkClickListener onOkClickListener) {
        if (dialogYunChaxun == null)
            dialogYunChaxun = new DialogYunChaxun(context);
        dialogYunChaxun.show(onOkClickListener);
    }

    public void showDialogOneButton(String title, String content, String buttonText, View.OnClickListener onClickListener) {
        if (dialogStandardOneButton == null) {
            dialogStandardOneButton = new DialogStandardOneButton(context);
            dialogStandardOneButton.setColorPrimary(colorPrimary);
            dialogStandardOneButton.setCornerRadius(cornerRadius);
        }
        dialogStandardOneButton.show(title, content, buttonText, onClickListener);
    }

    public void showDialogNewVersionHint(String versionName, String content, View.OnClickListener onUpdateClickLis, View.OnClickListener onCloseClickLis) {
        if (dialogNewVersionHint == null) {
            dialogNewVersionHint = new DialogNewVersionHint(context);
        }
        dialogNewVersionHint.show(versionName, content, onUpdateClickLis, onCloseClickLis);
    }

    public void showDialogAMapDownloading(DialogAMapDownloading.OnClickListener onClickListener) {
        if (dialogAMapDownloading == null)
            dialogAMapDownloading = new DialogAMapDownloading(context);
        dialogAMapDownloading.show(onClickListener);
    }

    public void dismissDialogAMapDownloading() {
        if (dialogAMapDownloading != null)
            dialogAMapDownloading.dismis();
    }

    public void updateDialogAMapDownloadingProgress(int progress) {
        if (dialogAMapDownloading != null)
            dialogAMapDownloading.updateProgress(progress);
    }

    public void showDialogTaskCompleteState(int allsize, int unchecksize, int checksize) {
        if (dialogTaskCompleteState == null)
            dialogTaskCompleteState = new DialogTaskCompleteState(context);
        dialogTaskCompleteState.show(allsize, unchecksize, checksize);
    }

    public void showDialogDonotKnowPosition(DialogDonotKnowPosition.OnDialogDonnotKnowPositionOperationListener onDialogDonnotKnowPositionOperationListener) {
        if (dialogDonotKnowPosition == null)
            dialogDonotKnowPosition = new DialogDonotKnowPosition(context);
        dialogDonotKnowPosition.show(onDialogDonnotKnowPositionOperationListener);
    }

    public void showDialogPandu(View.OnClickListener onCancel, DialogPandu.OnPanduSaveClickListener onPanduSaveClickListener) {
        if (dialogPandu == null) {
            dialogPandu = new DialogPandu(context);
        }
        dialogPandu.show(onCancel, onPanduSaveClickListener);
    }

    public void showKeyValueTable(ArrayMap<String, String> map) {
        if (dialogKeyValueTable == null)
            dialogKeyValueTable = new DialogKeyValueTable(context);
        dialogKeyValueTable.show(map);
    }

    public void showInput(String content, String leftButtonText, String rightButtonText, DialogInput.OnDialogInputClickListener leftListener, DialogInput.OnDialogInputClickListener rightListener) {
        if (dialogInput == null)
            dialogInput = new DialogInput(context);
        dialogInput.show(content, leftButtonText, rightButtonText, leftListener, rightListener);
    }

    public void showItems(List<String> itemNames, String cancelText,DialogInterface.OnCancelListener onCancelLis, AdapterView.OnItemClickListener onItemClickListener) {
        if (dialogTextItems == null){
            dialogTextItems = new DialogTextItems(context);
            dialogTextItems.setColorPrimary(colorPrimary);
            dialogTextItems.setCornerRadius(cornerRadius);
        }

        dialogTextItems.show(itemNames, cancelText,onCancelLis, onItemClickListener);
    }

    public void showAgreementHint(View.OnClickListener onUserAgreementClick, View.OnClickListener onPrivacyStatementClick, View.OnClickListener onyes, View.OnClickListener onno) {
        if (dialogAgreementHint == null)
            dialogAgreementHint = new DialogAgreementHint(context);
        dialogAgreementHint.show(onUserAgreementClick, onPrivacyStatementClick, onyes, onno);
    }

    public void showLoveHint(View.OnClickListener userAgreementClick, View.OnClickListener privacyStatement, View.OnClickListener onyes, View.OnClickListener onno) {
        if (dialogLoveHint == null) {
            dialogLoveHint = new DialogLoveHint(context);

        }
        dialogLoveHint.show(userAgreementClick, privacyStatement, onyes, onno);
    }

    public void showRealNameTaxDeclaration(View.OnClickListener onSpannableStringClick, View.OnClickListener onyes, View.OnClickListener onno) {
        if (dialogRealNameAuthAndTaxDeclaration == null)
            dialogRealNameAuthAndTaxDeclaration = new DialogRealNameAuthAndTaxDeclaration(context);

        dialogRealNameAuthAndTaxDeclaration.show(onSpannableStringClick, onyes, onno);
    }


    public void show() {
        if (dialogWaitting == null){
            dialogWaitting = new DialogWaitting(context);
            dialogWaitting.setColorPrimary(colorPrimary);
            dialogWaitting.setCornerRadius(cornerRadius);
        }

        if (dialogWaitting.isShowIng()) {
            if (queueWaitting == null)
                queueWaitting = new LinkedList<>();
            queueWaitting.add(0);
        } else {
            dialogWaitting.show();
        }
    }

    public void dismiss() {
        if (queueWaitting != null) {
            if (!queueWaitting.isEmpty()) {
                queueWaitting.poll();
            } else {
                if (dialogWaitting != null)
                    dialogWaitting.dismis();
            }
        } else {
            if (dialogWaitting != null)
                dialogWaitting.dismis();
        }
    }


    private DialogWarn dialogWarn;

    public void warn(String msg) {
        if (dialogWarn == null)
            dialogWarn = new DialogWarn(context);
        dialogWarn.show(msg);
    }

    private DialogError dialogError;

    public void error(String msg) {
        if (dialogError == null){
            dialogError = new DialogError(context);
            dialogError.setColorPrimary(colorPrimary);
            dialogError.setCornerRadius(cornerRadius);
        }

        dialogError.show(msg);
    }

    private DialogHint dialogHint;

    public void hint(String msg) {
        if (dialogHint == null)
            dialogHint = new DialogHint(context);
        dialogHint.show(msg);

    }


    //国测星绘 样式的 弹框
    private DialogStandardTwoButton dialogStandardTwoButton = null;

    public void showTwoButton(String content, String leftText, String rightText, DialogStandardTwoButton.OnClickListener leftButtonOnClickLis, DialogStandardTwoButton.OnClickListener rightButtonOnClickLis) {
        if (dialogStandardTwoButton == null) {
            dialogStandardTwoButton = new DialogStandardTwoButton(context);
            dialogStandardTwoButton.setColorPrimary(colorPrimary);
            dialogStandardTwoButton.setCornerRadius(cornerRadius);
        }
        dialogStandardTwoButton.show(content, leftText, rightText, leftButtonOnClickLis, rightButtonOnClickLis);
    }

    public void dismissTwoButton() {
        if (dialogStandardTwoButton != null) {
            dialogStandardTwoButton.dismis();
        }
    }


    public void dismissDialogDonotKnowPosition() {
        if (dialogDonotKnowPosition != null)
            dialogDonotKnowPosition.dismiss();
    }

    public void setColorPrimary(int colorPrimary) {
        this.colorPrimary = colorPrimary;
    }

    public void setCornerRadius(int cornerRadius) {
        this.cornerRadius = cornerRadius;
    }
}
